import { ResourceInfo } from "../info/ResourceInfo";
import { Log } from "../../debug/Log";
import { LoaderErrorEvent } from "../loader/LoaderErrorEvent";
type int = number;
//class StreamLoader
    
    export  class StreamLoader /* flash.events.EventDispatcher */
    {
       

        public initialize(arg1): void
        {
            if (!(arg1 is flash.utils.ByteArray)) 
            {
                throw new Error("Default Resource can only process ByteArrays!");
            }
            var loc1=new Object /* flash.display.Loader */();
            loc1.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE, this.onLoadComplete);
            loc1.contentLoaderInfo.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
            loc1.contentLoaderInfo.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
            loc1.loadBytes(arg1);
            return;
        }

        protected onContentReady(arg1): Boolean
        {
            return true;
        }

        /* internal  */onLoadComplete(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.target as Object /* flash.display.LoaderInfo */;
            this._loaderInfo = loc1;
            loc1.removeEventListener(flash.events.Event.COMPLETE, this.onLoadComplete);
            loc1.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
            loc1.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
            if (this.onContentReady(arg1 ? arg1.target.content : null)) 
            {
                this.onLoadCompleteHandler();
            }
            return;
        }

        protected onLoadCompleteHandler(): void
        {
            dispatchEvent(new Object /* flash.events.Event */(flash.events.Event.COMPLETE));
            return;
        }

        /* internal  */onChangeIOErrorHandler(arg1: Object /* flash.events.IOErrorEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.LoaderInfo */;
            loc1.removeEventListener(flash.events.Event.COMPLETE, this.onLoadComplete);
            loc1.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onChangeIOErrorHandler);
            loc1.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onChangeIOErrorHandler);
            this.onIOErrorHandler(arg1);
            return;
        }

        public dispose(): void
        {
            this.stop();
            this.clearVerifyTime();
            this._resourceInfo = null;
            this._url = null;
            this._loaderInfo = null;
            this._reloadTimes = 0;
            this._reloadSec = 10;
            this._maxReloadTimes = 10;
            this._isNeedReload = true;
            return;
        }

        public get url(): string
        {
            return this._url;
        }

        public get contentType(): string
        {
            if (this._loaderInfo) 
            {
                return this._loaderInfo.contentType;
            }
            return null;
        }

        public get loaderInfo(): Object /* flash.display.LoaderInfo */
        {
            return this._loaderInfo;
        }

        public stop(): void
        {
            if (this._urlLoader) 
            {
                this._urlLoader.removeEventListener(flash.events.Event.COMPLETE, this.onCompleteHandler);
                this._urlLoader.removeEventListener(flash.events.ProgressEvent.PROGRESS, this.onProgressHandler);
                this._urlLoader.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onIOError);
                this._urlLoader.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
                try 
                {
                    if (this._urlLoader.bytesLoaded != this._urlLoader.bytesTotal) 
                    {
                        this._urlLoader.close();
                    }
                }
                catch (e)
                {
                };
            }
            return;
        }

        public get isNeedReload(): Boolean
        {
            return this._isNeedReload;
        }

        public set isNeedReload(arg1: Boolean)
        {
            this._isNeedReload = arg1;
            return;
        }

        public get reloadSec(): int
        {
            return this._reloadSec;
        }

        public set reloadSec(arg1: int)
        {
            this._reloadSec = arg1;
            return;
        }

        public get maxReloadTimes(): int
        {
            return this._maxReloadTimes;
        }

        public set maxReloadTimes(arg1: int)
        {
            this._maxReloadTimes = arg1;
            return;
        }

        public get resourceInfo(): ResourceInfo
        {
            return this._resourceInfo;
        }

        public load(arg1: string, arg2: ResourceInfo): void
        {
            this._resourceInfo = arg2;
            this._url = arg1;
            this.initLoader();
            return;
        }

        /* internal  */initLoader(): void
        {
            if (this._url) 
            {
                this._urlLoader = new Object /* flash.net.URLLoader */();
                this._urlLoader.dataFormat = flash.net.URLLoaderDataFormat.BINARY;
                this._urlLoader.addEventListener(flash.events.Event.COMPLETE, this.onCompleteHandler);
                this._urlLoader.addEventListener(flash.events.ProgressEvent.PROGRESS, this.onProgressHandler);
                this._urlLoader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onIOError);
                this._urlLoader.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
                this._urlLoader.load(new Object /* flash.net.URLRequest */(this._url));
            }
            return;
        }

        protected removeLoadEvent(): void
        {
            this.clearVerifyTime();
            try 
            {
                this._urlLoader.removeEventListener(flash.events.Event.COMPLETE, this.onCompleteHandler);
                this._urlLoader.removeEventListener(flash.events.ProgressEvent.PROGRESS, this.onProgressHandler);
                this._urlLoader.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.onIOError);
                this._urlLoader.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.onIOError);
                this._urlLoader.close();
            }
            catch (e)
            {
            };
            return;
        }

        protected onCompleteHandler(arg1: Object /* flash.events.Event */): void
        {
            this.removeLoadEvent();
            this._urlLoader = null;
            this.streamloadComplete(arg1.target.data);
            this.initialize(arg1.target.data);
            return;
        }

        protected streamloadComplete(arg1): void
        {
            return;
        }

        protected onProgressHandler(arg1: Object /* flash.events.ProgressEvent */): void
        {
            dispatchEvent(arg1);
            if (this._isNeedReload) 
            {
                this.verifyTime();
            }
            return;
        }

        /* internal  */verifyTime(): void
        {
            this.clearVerifyTime();
            this._verifyTime = flash.utils.setTimeout(this.reload, this._reloadSec * 1000);
            return;
        }

        /* internal  */clearVerifyTime(): void
        {
            if (this._verifyTime) 
            {
                flash.utils.clearTimeout(this._verifyTime);
                this._verifyTime = 0;
            }
            return;
        }

        /* internal  */reload(): void
        {
            var loc1;
            var loc2=((loc1 = this)._reloadTimes + 1);
            loc1._reloadTimes = loc2;
            this.clearVerifyTime();
            this.stop();
            if (this._reloadTimes < this._maxReloadTimes) 
            {
                Log.debug(this._url + "重新加载" + this._reloadTimes + "次");
                this.initLoader();
            }
            else 
            {
                Log.error(this._url + "重新加载失败");
                this.onIOError(new Object /* flash.events.ErrorEvent */("重新加载失败"));
            }
            return;
        }

        /* internal  */onIOError(arg1: Object /* flash.events.ErrorEvent */): void
        {
            this.removeLoadEvent();
            this.onIOErrorHandler(arg1);
            return;
        }

        protected onIOErrorHandler(arg1: Object /* flash.events.ErrorEvent */): void
        {
            Log.system(arg1.text);
            dispatchEvent(new LoaderErrorEvent(arg1.text));
            return;
        }

        private /* var */_url: string;

        private /* var */_urlLoader: Object /* flash.net.URLLoader */;

        private /* var */_contentType: string;

        private /* var */_loaderInfo: Object /* flash.display.LoaderInfo */;

        protected /* var */_resourceInfo: ResourceInfo;

        private /* var */_isNeedReload: Boolean=true;

        protected /* var */_verifyTime: int;

        protected /* var */_reloadTimes: int=0;

        private /* var */_maxReloadTimes: int=10;

        private /* var */_reloadSec: int=10;
    }
